//
//  ZCRaiseCell.m
//  zctx
//
//  Created by 郭亚京 on 15/10/9.
//  Copyright © 2015年 sanliang. All rights reserved.
//

#import "ZCRaiseCell.h"

@interface ZCRaiseCell() <UITextViewDelegate>

@end

@implementation ZCRaiseCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier cellType:(RaiseCellType)type {
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        if (type == RaiseCellTypeInput) {
            [self addLabelTitle];
            [self addTextField];
            [self addOptionalLable];
        } else if (type == RaiseCellTypeSelect) {
            [self addLabelTitle];
            [self addButtonProvince];
            [self addButtonCity];
        } else if (type == RaiseCellTypePush) {
            [self addLabelTitle];
            [self addLabelInfo];
            [self addrightImgView];
        } else if (type == RaiseCellTypeText) {
            [self.contentView addSubview:self.labelTitle];
            [self.labelTitle mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.contentView.top).with.offset(11);
                make.height.mas_equalTo(21);
                make.left.equalTo(self.contentView.left).with.offset(20);
                make.width.mas_equalTo(75);
            }];
            [self addTextView];
            
        }else if(type ==RaiseCellTypePrompt){
            [self addPromptView];
        }else if(type ==RaiseCellTypeSao){
            [self addLabelTitle];
            [self addSaoButton];
            [self.contentView addSubview:self.textField];
            [self.textField mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(self.contentView.centerY);
                make.height.mas_equalTo(@(30));
                make.left.equalTo(self.labelTitle.right).with.offset(0);
                make.right.equalTo(self.saoButton.left).with.offset(-10);
            }];

           
        }else if (type == RaiseCellTypemore){
            [self.contentView addSubview:self.rightImgView];
            [self.rightImgView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(self.contentView.centerY);
                make.height.mas_equalTo(7);
                make.right.equalTo(self.contentView.right).with.offset(-15);
                make.width.mas_equalTo(13);
            }];
            _rightImgView.image = [UIImage imageNamed:@"raise_right_shang"];
            [self addTitleButtonS];
            self.backgroundColor = [UIColor colorWithHexString:@"#fafcfb"];
        }
    }
    return self;
}

-(void)setIndexPath:(NSIndexPath *)indexPath{
    _indexPath = indexPath;
        if (indexPath.row==4) {
             [self.labelTitle setText:@"产品分类："];
        }else if (indexPath.row == 5) {
            //检测机构
            [self.labelTitle setText:@"检测项目："];
            [self.labelInfo setText:@"选择检测项目"];
        } else if(indexPath.row==6){
            //检测项目
            [self.labelTitle setText:@"检测机构："];
            [self.labelInfo setText:@"选择检测机构"];
        }else if(indexPath.row ==7){
            self.accessoryType = UITableViewCellAccessoryNone;
            [self.labelTitle setText:@"检测金额："];
            [self.labelInfo setTextColor:[UIColor redColor]];
            [self.labelInfo setText:@""];
        }
}

- (void)addTextView {
    [self.contentView addSubview:self.textView];
    [self.textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView.top).with.offset(11);
        make.bottom.equalTo(self.contentView.bottom).with.offset(-11);
        make.left.equalTo(self.labelTitle.right).with.offset(0);
        make.right.equalTo(self.contentView.right).with.offset(-20);
    }];
}

- (void)addLabelTitle {
    [self.contentView addSubview:self.labelTitle];
    [self.labelTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.contentView.centerY);
        make.height.equalTo(self.contentView.height);
        make.left.mas_equalTo(@(20));
        make.width.mas_equalTo(@(75));
    }];
}

- (void)addOptionalLable {
    [self.contentView addSubview:self.optionalLable];
    [self.optionalLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.contentView.centerY);
        make.height.equalTo(self.contentView.height);
        make.right.mas_equalTo(@(-19));
        make.width.mas_equalTo(@(30));
    }];
}

-(void)addTitleButtonS{
     [self.contentView addSubview:self.titleButton];
//    self.titleButton.backgroundColor = [UIColor orangeColor];
    self.titleButton.titleEdgeInsets = UIEdgeInsetsMake(0,0, 0,-10);
     [self.titleButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.contentView.centerY);
        make.height.equalTo(self.contentView.height);
        make.width.mas_equalTo(80);
        make.right.equalTo(self.rightImgView.left).with.offset(0);
    }];
}

- (void)addTextField {
    [self.contentView addSubview:self.textField];
    [self.textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.contentView.centerY);
        make.height.mas_equalTo(@(30));
        make.left.equalTo(self.labelTitle.right).with.offset(0);
        make.width.mas_equalTo(@(SCREEN_WIDTH/2));
    }];
}

- (void)addButtonProvince {
    [self.contentView addSubview:self.buttonProvince];
    [self.buttonProvince mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.contentView.centerY);
        make.height.equalTo(self.contentView.height).with.offset(-10);
        make.left.mas_equalTo(@(120));
        make.width.mas_equalTo(@(80));
    }];
}

- (void)addButtonCity {
    [self.contentView addSubview:self.buttonCity];
    [self.buttonCity mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.contentView.centerY);
        make.height.equalTo(self.contentView.height).with.offset(-10);
        make.left.mas_equalTo(@(220));
        make.width.mas_equalTo(@(80));
    }];
}

- (void)addLabelInfo {
    [self.contentView addSubview:self.labelInfo];
    [self.labelInfo mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.contentView.centerY);
        make.height.equalTo(self.contentView.height).with.offset(-10);
        make.left.equalTo(self.labelTitle.right).with.offset(0);
        make.width.mas_equalTo(@(SCREEN_WIDTH-120));
    }];
}

-(void)addPromptView{
    UILabel *PromptLable = [[UILabel alloc]init];
    PromptLable.numberOfLines = 0;
    [self.contentView addSubview:PromptLable];
    [PromptLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.contentView.centerY);
        make.height.equalTo(self.contentView.height);
        make.left.equalTo(self.contentView.left).with.offset(18);
        make.right.equalTo(self.contentView.right).with.offset(-18);
    }];
    PromptLable.text = @"温馨提示:检测金额为检测费用,不包含样品费用,最终的金额以审核过后的金额为准。";
    PromptLable.textColor = [UIColor colorWithHexString:@"#8a8a8a"];
    PromptLable.font = [UIFont systemFontOfSize:12];
}

-(void)addrightImgView{
    [self.contentView addSubview:self.rightImgView];
    [self.rightImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.contentView.centerY);
        make.height.mas_equalTo(13);
        make.right.equalTo(self.contentView.right).with.offset(-15);
        make.width.mas_equalTo(7);
    }];
}

-(void)addSaoButton{
    
    self.saoButton = [[SDItemButton alloc]initWithFrame:CGRectZero];
    self.saoButton.title = @"扫条码";
    self.saoButton.titleColor = MainColor;
    self.saoButton.imageName = @"raise_sao";
    [self.contentView addSubview:self.saoButton];
    [self.saoButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.contentView.centerY);
        make.right.equalTo(self.contentView.right).with.offset(-20);
        make.width.mas_equalTo(@(35));
        make.height.mas_equalTo(@(42));
    }];
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    
}

#pragma mark - UITextViewDelegate Methods

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView {
    if ([textView.text isEqualToString:RaiseCellTextViewInfo]) {
        textView.text = @"";
    }
    if (self.blockTextViewShouldBeginEdit) {
        self.blockTextViewShouldBeginEdit();
    }
    return YES;
}

- (void)textViewDidEndEditing:(UITextView *)textView {
    if (textView.text.length == 0) {
        textView.text = RaiseCellTextViewInfo;
    }
    if (self.blockTextViewDidEndEdit) {
        self.blockTextViewDidEndEdit();
    }
}

#pragma mark - button click

- (void)provinceButtonClick {
    if (self.blockProvinceButtonClick) {
        self.blockProvinceButtonClick();
    }
}
- (void)cityButtonClick {
    if (self.blockCityButtonClick) {
        self.blockCityButtonClick();
    }
}
#pragma mark - instance 

- (UILabel *)labelTitle {
    if (!_labelTitle) {
        _labelTitle = [[UILabel alloc] init];
        [_labelTitle setTextColor:[UIColor colorWithHexString:@"#404040"]];
        _labelTitle.font = [self titleFont];
    }
    return _labelTitle;
}
- (UILabel *)optionalLable {
    if (!_optionalLable) {
        _optionalLable = [[UILabel alloc] init];
        _optionalLable.text = @"选填";
        _optionalLable.textAlignment = NSTextAlignmentRight;
        _optionalLable.font = [UIFont systemFontOfSize:15];
        [_optionalLable setTextColor:[UIColor colorWithHexString:@"#a3a3a3"]];
    }
    return _optionalLable;
}

- (UITextField *)textField {
    if (!_textField) {
        _textField = [[UITextField alloc] init];
        _textField.font = [UIFont systemFontOfSize:15];
//        [_textField setBackgroundColor:[UIColor colorWithHexString:@"f2f2f2"]];
        
    }
    return _textField;
}

- (UIButton *)buttonProvince {
    if (!_buttonProvince) {
        _buttonProvince = [UIButton buttonWithType:UIButtonTypeCustom];
        [_buttonProvince setTitle:@"省份" forState:UIControlStateNormal];
        [_buttonProvince setTitleColor:[UIColor colorWithHexString:@"666666"] forState:UIControlStateNormal];
        _buttonProvince.titleLabel.font = [UIFont systemFontOfSize:15];
        [_buttonProvince setImage:[UIImage imageNamed:@"raise_san"] forState:UIControlStateNormal];
        [_buttonProvince.imageView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(_buttonProvince.bottom).with.offset(0);
            make.right.equalTo(_buttonProvince.right).with.offset(0);
            make.width.mas_equalTo(@(10));
            make.height.mas_equalTo(@(10));
        }];
        [_buttonProvince addTarget:self action:@selector(provinceButtonClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _buttonProvince;
}

- (UIButton *)buttonCity {
    if (!_buttonCity) {
        _buttonCity = [UIButton buttonWithType:UIButtonTypeCustom];
        [_buttonCity setTitle:@"城市" forState:UIControlStateNormal];
        [_buttonCity setTitleColor:[UIColor colorWithHexString:@"666666"] forState:UIControlStateNormal];
        _buttonCity.titleLabel.font = [UIFont systemFontOfSize:15];
        [_buttonCity setImage:[UIImage imageNamed:@"raise_san"] forState:UIControlStateNormal];
        [_buttonCity.imageView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(_buttonCity.bottom).with.offset(0);
            make.right.equalTo(_buttonCity.right).with.offset(0);
            make.width.mas_equalTo(@(10));
            make.height.mas_equalTo(@(10));
        }];
        [_buttonCity addTarget:self action:@selector(cityButtonClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _buttonCity;
}

- (UILabel *)labelInfo {
    if (!_labelInfo) {
        _labelInfo = [[UILabel alloc] init];
        [_labelInfo setTextColor:[UIColor colorWithHexString:@"#000000"]];
        _labelInfo.textAlignment = NSTextAlignmentLeft;
        _labelInfo.font = [self titleFont];
    }
    return _labelInfo;
}

- (UITextView *)textView {
    if (!_textView) {
        _textView = [[RPFloatingPlaceholderTextView alloc] initWithFrame:CGRectZero];
        [_textView setBackgroundColor:[UIColor colorWithHexString:@"f2f2f2"]];
        [_textView setTextColor:[UIColor colorWithHexString:@"a3a3a3"]];
        [_textView setFont:[UIFont systemFontOfSize:15]];
         self.textView.placeholder =[NSString stringWithFormat:@"食品安全我做主，小伙伴们快来支持我~\n一起来参与食品安全检测..."];
    }
    return _textView;
}

-(UIImageView *)rightImgView{
    
    if (!_rightImgView) {
        _rightImgView = [[UIImageView alloc]init];
        _rightImgView.image = [UIImage imageNamed:@"raise_right"];
    }
    return _rightImgView;
}

-(UIButton *)titleButton{
    if (!_titleButton) {
        _titleButton = [[UIButton alloc] init];
        _titleButton.titleLabel.font = [UIFont systemFontOfSize:15];
        [_titleButton setTitle:@"更多选项" forState:UIControlStateNormal];
        [_titleButton setTitleColor:[UIColor colorWithHexString:@"#404040"] forState:UIControlStateNormal];
        [_titleButton addTarget:self action:@selector(ClickTitleButton:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _titleButton;
}

-(UIFont *)titleFont{
    return [UIFont systemFontOfSize:15];
}

-(void)ClickTitleButton:(UIButton *)sender{
    NSLog(@"sdfs");
    if (_delegate && [_delegate respondsToSelector:@selector(didSeclectButtonClick:)]) {
        [_delegate didSeclectButtonClick:self.indexPath];
    }
}

@end
